﻿using FreeSql;
using Simple.Repository;

namespace Simple.WebAi.Extensions
{
    /// <summary>
    /// FreeSql
    /// </summary>
    public static class FreeSqlExtensions
    {
        /// <summary>
        /// 注册FreeSql
        /// </summary>
        /// <param name="services"></param>
        public static void AddFreeSql(this IServiceCollection services)
        {
            var configuration = services.BuildServiceProvider().GetService<IConfiguration>();
            var btcActiveConnectionString = configuration!.GetConnectionString("SimpleMySqlConnection");

            var fsql = new FreeSqlCloud();
            fsql.DistributeTrace = log => Console.WriteLine(log.Split('\n')[0].Trim());
            fsql.Register(DbEnum.Simple, () => new FreeSqlBuilder().UseConnectionString(DataType.MySql, btcActiveConnectionString).UseMonitorCommand(cmd => Console.WriteLine($"\nActive - Sql：\n{cmd.CommandText}")).Build());
            services.AddSingleton<IFreeSql>(fsql);
            services.AddSingleton(fsql);
        }
    }
}
